﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dycox.Collections;
using Dycox.ComponentModel;
using Dycox.Utils;

namespace Dycox.Reporting
{
    public class DataSheetColumn
    {

        public DataSheetColumn()
        {

        }

        public DataSheetColumn(ViewField vf)
        {
            if (vf == null)
                throw new ArgumentNullException("vf");

            _headerText = vf.Label;
            //_format = vf.Format;
            _group = vf.Category;
            _width = vf.Size;
            _expression = vf.PropertyName;

        }

        private string _headerText;

        public string HeaderText
        {
            get { return _headerText; }
            set { _headerText = value; }
        }


        private DataSheetConfig _dataSheet;

        public DataSheetConfig DataSheet
        {
            get { return _dataSheet; }
            internal set { _dataSheet = value; }
        }

        private string _expression;

        public string Expression
        {
            get { return _expression; }
            set { _expression = value; }
        }

        private int _width;

        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        private Style _headerStyle;

        public Style HeaderStyle
        {
            get { return _headerStyle; }
            set { _headerStyle = value; }
        }

        private Style _bodyStyle;

        public Style BodyStyle
        {
            get { return _bodyStyle; }
            set { _bodyStyle = value; }
        }

        private Style _footerStyle;

        public Style FooterStyle
        {
            get { return _footerStyle; }
            set { _footerStyle = value; }
        }

        private string _group;

        public string Group
        {
            get { return _group; }
            set { _group = value; }
        }

        public object GetValue(object dataItem)
        {
            if (dataItem == null) return null;

            object value = DataBinderUtil.Eval(dataItem, _expression);

            return value;
        }

        //public string GetText(object dataItem)
        //{
        //    if (dataItem == null) return string.Empty;

        //    string format = _format;

        //    if (!string.IsNullOrEmpty(format) && format.IndexOf("{0:") < 0)
        //        format = "{0:" + format + "}";

        //    return DataBinderUtil.Eval(dataItem, _expression, format);
        //}
    }

    public class DataSheetColumnCollection : OwnedCollection<DataSheetColumn, DataSheetConfig>
    {
        public DataSheetColumnCollection(DataSheetConfig owner)
            : base(owner)
        {

        }

        protected override void SetItemOwner(DataSheetColumn item, DataSheetConfig owner)
        {
            item.DataSheet = owner;
        }

        protected override DataSheetConfig GetItemOwner(DataSheetColumn item)
        {
            return item.DataSheet;
        }

        public DataSheetColumn Add(ViewField vf)
        {
            DataSheetColumn col = new DataSheetColumn(vf);
            this.Add(col);
            return col;
        }

        public DataSheetColumn Add(string header, string expression)
        {
            DataSheetColumn col = new DataSheetColumn();
            col.HeaderText = header;
            col.Expression = expression;

            this.Add(col);

            return col;
        }
    }
}
